// pages/statisticsDay/statisticsDay.js
const app = getApp();
const util = require("../../utils/util.js");
const timeTool = require('../../utils/timeTool.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orgid:'',
    orgname:'',
    orgurl:'',
    reportDate:'',
    type:0,
    number:{},
    list:[],
    isorg:null,
    pageNumber:1,
    totalPages:1,
  },
  tab(e){
    var that = this
    var type = e.currentTarget.dataset.type
    if(type!=that.data.type){
      that.setData({
        type:type,
        list:[],
        pageNumber:1,
      })
      if (that.data.isorg == 1) {
        that.getorg()
      } else if (that.data.isorg == 0) {
        that.getpersonal()
      }
    }
  },
  topage(e){
    var that = this
    var relationid = e.currentTarget.dataset.relationid
    var createUid = e.currentTarget.dataset.createuid
    var id = e.currentTarget.dataset.id
    var avaimg = e.currentTarget.dataset.avaimg
    var date = util.itemDateToShow(e.currentTarget.dataset.date,1,false,false)
    app.req({
      url: "orgmini/epidemic/reportCount",
      data: {
        relationId: relationid,
        createUid: createUid,
        date: date
      },
      success: function (res) {
        if(res.code==200){
          if(res.data.count>1){
            wx.navigateTo({
              url: '/pages/statisticsDetailsList/statisticsDetailsList?date=' + date + '&createUid=' + createUid + '&relationid=' + relationid +'&isorg='+that.data.isorg,
            })
          }else{
            wx.navigateTo({
              url: '/pages/epidemicSee/epidemicSee?id=' + relationid + '&isshow=' + that.data.isorg + '&listid=' + id + '&isorg=' + that.data.isorg + '&avaimg=' + avaimg,
            })
          }
        }
      }
    })
  },
  getorg() {
    var that = this
    app.req({
      url: "orgmini/epidemic/orgPersonalStatisticsByDay",
      data: {
        orgId: that.data.orgid,
        pageNumber: that.data.pageNumber,
        pageSize: 10,
        reportDate: that.data.reportDate,
        type:that.data.type*1 +1,
      },
      success: function (res) {
        if (200 == res.code) {
          if (that.data.pageNumber == 1) {
            that.setData({
              list: res.data.page.rows
            })
          } else {
            that.setData({
              list: that.data.list.concat(res.data.page.rows)
            })
          }
          that.setData({
            number: res.data.number,
            totalPages: res.data.page.totalPages
          })
        }
      }
    });
  },
  getpersonal() {
    var that = this
    app.req({
      noLoading: true,
      url: "orgmini/epidemic/personalStatisticsByDay",
      data: {
        pageNumber: that.data.pageNumber,
        pageSize: 10,
        reportDate: that.data.reportDate,
        type: that.data.type * 1 + 1,
      },
      success: function (res) {
        if (200 == res.code) {
          if (that.data.pageNumber == 1) {
            that.setData({
              list: res.data.page.rows
            })
          } else {
            that.setData({
              list: that.data.list.concat(res.data.page.rows)
            })
          }
          that.setData({
            number: res.data.number,
            totalPages: res.data.page.totalPages
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      orgid: options.orgid,
      orgname:options.orgname,
      orgurl:options.orgurl,
      reportDate: options.reportdate,
      isorg:options.isorg
    })
    wx.setNavigationBarTitle({
      title: parseInt(that.data.reportDate.split('-')[1])+'月'+ parseInt(that.data.reportDate.split('-')[2]) + '日' + ' 疫情详情'
    })
    if (options.follow*1 >0){
      that.setData({
        type:1,
        list:[],
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    if(that.data.isorg==1){
      that.getorg()
    }else if(that.data.isorg==0){
      that.getpersonal()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if (that.data.totalPages > that.data.pageNumber) {
      that.setData({
        pageNumber: that.data.pageNumber + 1
      })
      if (that.data.isorg == 1) {
        that.getorg()
      } else if (that.data.isorg == 0) {
        that.getpersonal()
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})